package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.Assignment;
import cn.thoughtworks.school.programCenter.entities.AssignmentQuiz;
import cn.thoughtworks.school.programCenter.entities.ReviewQuiz;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.repositories.AssignmentQuizRepository;
import cn.thoughtworks.school.programCenter.repositories.AssignmentRepository;
import cn.thoughtworks.school.programCenter.repositories.ReviewQuizRepository;
import cn.thoughtworks.school.programCenter.services.QuizCenterService;
import cn.thoughtworks.school.programCenter.services.ReviewQuizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api")
public class BasicQuizController {

    @Autowired
    private QuizCenterService quizCenterService;
    @Autowired
    private ReviewQuizService reviewQuizService;
    @Autowired
    private ReviewQuizRepository reviewQuizRepository;
    @Autowired
    private AssignmentRepository assignmentRepository;
    @Autowired
    private AssignmentQuizRepository assignmentQuizRepository;

    @RequestMapping(value = "/v2/assignments/{assignmentId}/answers", method = RequestMethod.POST)
    public ResponseEntity submitBasicQuizAnswer(@PathVariable Long assignmentId,
                                                @RequestBody List<Map> submitAnswers,
                                                @Auth User current) throws Exception {
        Long studentId = current.getId();
        if (reviewQuizService.isSubmitted(assignmentId, current.getId())) {
            throw new BusinessException("Please do not resubmit!");
        }

        List quizSubmissionList = quizCenterService.submitBasicQuizAnswer(assignmentId, submitAnswers, studentId);
        int correctCount = quizSubmissionList.stream().filter(quizSubmissionStatus -> quizSubmissionStatus.equals(true)).toArray().length;
        int grade = (int) Math.rint((correctCount / (double) quizSubmissionList.size()) * 100);

        Assignment assignment = assignmentRepository.findById(assignmentId)
            .orElseThrow(() -> new BusinessException(String.format("Unknown assignment with id: %s", assignmentId)));

        Long taskId = assignment.getTaskId();

        String status = grade > 85 ? "优秀" : "已完成";
        ReviewQuiz reviewQuiz = new ReviewQuiz(studentId, assignmentId, taskId, grade, status, new Date());
        reviewQuizRepository.save(reviewQuiz);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping("/assignments/{assignmentId}/assignmentsQuiz")
    public ResponseEntity dragSort(@PathVariable Long assignmentId, @RequestBody List<Map> quizzes) {
        List<AssignmentQuiz> assignmentQuizs = new ArrayList<>();
        for (int i = 0; i < quizzes.size(); ++i) {
            AssignmentQuiz assignmentQuiz = assignmentQuizRepository.findByAssignmentIdAndQuizId(assignmentId, Long.valueOf(quizzes.get(i).get("id").toString()));
            assignmentQuiz.setOrderNumber((long) i);
            assignmentQuizs.add(assignmentQuiz);
        }
        assignmentQuizRepository.saveAll(assignmentQuizs);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
